package com.剑指offer.第七章队列;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class 二叉树的右侧视图 {

    public static List<Integer> resolve(TreeNode treeNode) {
        List<Integer> result = new ArrayList<>();
        if (treeNode == null) {
            return null;
        }

        Queue<TreeNode> queue1 = new LinkedList<>();
        Queue<TreeNode> queue2 = new LinkedList<>();

        queue1.offer(treeNode);

        while (!queue1.isEmpty()) {
            TreeNode poll = queue1.poll();

            if (poll.left != null) {
                queue2.offer(poll.left);
            }

            if (poll.right != null) {
                queue2.offer(poll.right);
            }

            // 当queue1为空时，最后一个节点就是，右边的节点
            if (queue1.isEmpty()) {
                result.add(poll.val);
                queue1 = queue2;
                queue2 = new LinkedList<>();
            }

        }
        return result;
    }

}
